﻿// 网图绘制

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using static AdjPlane.Angle_t;

namespace PlaneADJ
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        List<Point2D> Points = new List<Point2D>();
        List<Line2D> ObsL = new List<Line2D>();
        List<Line2D> ObsS = new List<Line2D>();
        readonly char[] splitCol = { ',', '，', '\t', ' ' };//用于分割每一列，\t:制表符

        static string[] mysplit(string line, char[] split)
        {
            return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
        }
        void ReadGraph(string path)
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//注册gb2312  
            string[] lines = File.ReadAllLines(path, Encoding.GetEncoding("gb2312"));
            string[] count = mysplit(lines[0], splitCol);
            int pointnum = Convert.ToInt32(count[0]);
            int linenum = Convert.ToInt32(count[1]);
            for (int i = 1; i < pointnum + 1; i++)
            {
                string[] str = mysplit(lines[i], splitCol);
                Point2D p = new Point2D();
                p.name = str[0].Trim();
                p.x = double.Parse(str[1]);
                p.y = double.Parse(str[2]);
                if (str.Length == 3)
                {
                    p.E = p.F = p.T = 0;
                }
                else
                {
                    p.E = double.Parse(str[3]);
                    p.F = double.Parse(str[4]);
                    p.T = double.Parse(str[5]);
                }
                Points.Add(p);
            }
            for (int i = pointnum + 1; i < 1+pointnum+linenum; i++)
            {
                string[] str = mysplit(lines[i], splitCol);
                Line2D line = new Line2D();
                line.beg = str[0];
                line.end = str[1];
                if (str[2].Contains("L"))
                {
                    line.sign = "L";
                    ObsL.Add(line);
                }
                else if (str[2].Contains("S"))
                {
                    line.sign = "S";
                    ObsS.Add(line);
                }

            }
        }
        int FindPoint(string name)
        {
            int i;
            for (i = 0; i < Points.Count; i++)
            {
                if (name == Points[i].name)
                    return i;
            }
            return -1;
        }
        ScottPlot.Plot plot1;
        void PlotNet()
        {
            plot1 = formsPlot1.Plot;
            double dx = 2, dy = 2;
            plot1.XLabel("Y");
            plot1.YLabel("X");
            for (int i = 0; i < Points.Count; i++)
            {
                string name = Points[i].name;
                double x = Points[i].y;
                double y = Points[i].x;
                plot1.AddPoint(x, y, null, 1);
                plot1.AddText(name, x + dx, y + dy);
            }
            for (int i = 0; i < ObsL.Count; i++)
            {
                string staName = ObsL[i].beg;
                string obsName = ObsL[i].end;
                int j = FindPoint(staName);
                int k = FindPoint(obsName);
                if (j == -1 || k == -1) continue;
                double x1 = Points[j].y, x2 = Points[k].y;
                double y1 = Points[j].x, y2 = Points[k].x;
                double[] xs = { x1, x2 };
                double[] ys = { y1, y2 };
                plot1.AddScatterLines(xs, ys,Color.Black);
                //plot1.AddLine(x1, y1, x2, y2);
            }
            for (int i = 0; i < ObsS.Count; i++)
            {
                string staName = ObsS[i].beg;
                string obsName = ObsS[i].end;
                int j = FindPoint(staName);
                int k = FindPoint(obsName);
                if (j == -1 || k == -1) continue;
                double x1 = Points[j].y, x2 = Points[k].y;
                double y1 = Points[j].x, y2 = Points[k].x;
                double[] xs = { x1, x2 };
                double[] ys = { y1, y2 };
                plot1.AddScatterLines(xs, ys,Color.Black);
                //plot1.AddLine(x1, y1, x2, y2);
            }
            plot1.AxisScaleLock(true);
            //plot1.MatchLayout(plot1);
            formsPlot1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Multiselect = false;
            op.Filter = "Text Files(.gra)|*.gra";
            op.Title = "选择文件";
            op.ShowDialog(this);
            string path = op.FileName;
            if (path.Length == 0) { }
            else
            {
                try
                {
                    ReadGraph(path);
                    PlotNet();
                }
                catch { };
            }
        }

        void PlotEllipse()
        {
            for (int i = 0; i < Points.Count; i++)
            {
                double x0 = Points[i].x;
                double y0 = Points[i].y;
                double E = Points[i].E * 100;
                double F = Points[i].F * 100;
                double T = D2RD(DMS2D( Points[i].T));
                //double[] p = { Math.Cos(T), -Math.Sin(T), Math.Sin(T), Math.Cos(T) };
                List<double> xs = new List<double>();
                List<double> ys = new List<double>();
                for (double j = 0; j < 2 * Math.PI; j += 0.01)
                {
                    double x1 = E * Math.Cos(j);
                    double y1 = F * Math.Sin(j);
                    double x2 = Math.Cos(T) * x1 - Math.Sin(T) * y1;
                    double y2 = Math.Sin(T) * x1 + Math.Cos(T) * y1;
                    double x3 = x0 + x2;
                    double y3 = y0 + y2;
                    xs.Add(y3);
                    ys.Add(x3);
                }
                plot1.AddScatterLines(xs.ToArray(), ys.ToArray(),Color.Blue);
            }
            plot1.AxisScaleLock(true);
            formsPlot1.Refresh();
        }
        int isplotEllipse;
        private void button2_Click(object sender, EventArgs e)
        {
            if (plot1 == null)
                return;
            if (isplotEllipse == 0)
            {
                PlotEllipse();
                isplotEllipse = 1;
            }
            else
            {
                plot1.Clear();
                PlotNet();
                isplotEllipse = 0;
            }
        }

        private void formPlot1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (plot1 != null)
            {
                (double a, double b) = plot1.GetCoordinate(0, 0);
                (double c, double d) = plot1.GetCoordinate(0, 1);
                double dis = Math.Sqrt((a - c) * (a - c) + (b - d) * (b - d));
                string ratio = string.Format("{0}:{1}", 1, dis.ToString("f2"));
                label1.Text = ratio;
            }
        }
    }


    public class Point2D
    {
        public string name;
        public double x;
        public double y;
        public double E;
        public double F;
        public double T;
    }
    public class Line2D
    {
        public string beg;
        public string end;
        public string sign;
    }
}
